package com.tinckay.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tinckay.common.util.LocalDateTimeUtils;
import com.tinckay.common.util.WrapperUtils;
import com.tinckay.entity.dto.TransportDto;
import com.tinckay.entity.po.StorehousePlan;
import com.tinckay.entity.po.Task;
import com.tinckay.entity.po.TranProcess;
import com.tinckay.entity.po.User;
import com.tinckay.mapper.TaskMapper;
import com.tinckay.mapper.TranProcessMapper;
import com.tinckay.service.StorehousePlanService;
import com.tinckay.service.TaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jean
 * @since 2018-10-21
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    private TranProcessMapper tranProcessMapper;
    @Autowired
    private StorehousePlanService storehousePlanService;

    @Override
    public TransportDto tranInfo(String transportTime) {
        TransportDto transportDto = null;

        List<Task> taskList = this.baseMapper.tranInfo(WrapperUtils.dataScopeWrapper(WrapperUtils.USERID_FLAG_ONE), transportTime);

        if (CollectionUtil.isNotEmpty(taskList)) {
            transportDto = new TransportDto();
            transportDto.setTranCount(taskList.size());

            long totalWasteTime = taskList.stream().mapToLong(Task::getConsumeTime).sum();
            transportDto.setTotalWasteTime(totalWasteTime);
            transportDto.setTimePerRequest(totalWasteTime / taskList.size());
            transportDto.setTaskDtoList(taskList);
        }

        return transportDto;
    }

    @Override
    public List<Map<String, Object>> countTask(Integer tranTimes, String workDate, String barnNo, String vehNo) {
        QueryWrapper dataScopeWrapper = WrapperUtils.dataScopeWrapper(WrapperUtils.USERID_FLAG_TWO);
        return this.baseMapper.countTask(dataScopeWrapper, tranTimes, workDate, barnNo, vehNo);
    }

    @Override
    public Task taskInfo(long taskId) {
        return baseMapper.taskInfo(taskId);
    }

    @Override
    public Map<String, Object> latestTransport(String vehNo) {
        Map<String, Object> resultMap = baseMapper.latestTransport(vehNo);
        Object id = resultMap.get("id");
        if (null != id) {
            List<TranProcess> tranProcesses = tranProcessMapper.getTranProcessByTaskId((int) id);
            resultMap.put("tranProcesses", tranProcesses);
        }
        return resultMap;
    }

    @Override
    public List<Task> vehTranProcess(String vehNo) {
        return baseMapper.vehTranProcess(vehNo);
    }

    @Override
    public List<Map<String, Object>> pourProcessTimeInfo() {
        List<Map<String, Object>> result = null;
        //TODO:测试
        LambdaQueryWrapper<StorehousePlan> wrapper = new QueryWrapper<StorehousePlan>()
                .lambda()
                .eq(StorehousePlan::getPlanTime, LocalDate.now());
        //.eq(StorehousePlan::getStoreState,0)

        List<StorehousePlan> storehousePlanList = storehousePlanService.list(wrapper);
        if (CollectionUtil.isNotEmpty(storehousePlanList)) {
            List<String> storeCodeList = storehousePlanList.stream().map(StorehousePlan::getStoreCode).collect(Collectors.toList());
            result = storehousePlanService.countPlan(storeCodeList);
        }

        return result;
    }

}
